package com.sijibao.gather.consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Start
{

	private static final Logger logger = LoggerFactory.getLogger(Start.class);

	private static volatile boolean running = true;

	public static void main(String[] args)
	{
		try
		{
			Container container = new SpringContainer();

			Runtime.getRuntime().addShutdownHook(new Thread()
			{
				public void run()
				{

					try
					{
						container.stop();
						logger.info(
								"consumer " + container.getClass().getSimpleName()
										+ " stopped!");
					} catch (Throwable t)
					{
						logger.error(t.getMessage(), t);
					}
					synchronized (Start.class)
					{
						running = false;
						Start.class.notify();
					}

				}
			});

			container.start();
			logger.info("consumer " + container.getClass().getSimpleName()
					+ " started!");
			System.out.println("consumer " + container.getClass().getSimpleName()
					+ " started!");

		} catch (RuntimeException e)
		{
			e.printStackTrace();
			logger.error(e.getMessage(), e);
			System.exit(1);
		}
		synchronized (Start.class)
		{
			while (running)
			{
				try
				{
					Start.class.wait();
				} catch (Throwable e)
				{
					e.printStackTrace();
					logger.error(e.getMessage(), e);
				}
			}
			logger.info("consumer is shutdown all success!");
		}
	}
}
